package com.alasbusy.alasbusyapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.twoactivity.NewsDetailActivity;
import com.alasbusy.alasbusyapp.bean.HomeNewsKejiBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/10.
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<HomeNewsKejiBean.ResultBean.DataBean> mData = new ArrayList<>();
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    public static final int LOADING_NO = 2;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;
    private static final int TYPE_HEAD = 0;  //普通Item View
    private static final int TYPE_ITEM = 1;  //普通Item View
    private static final int TYPE_FOOTER = 2;  //底部FootView
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private LayoutInflater mInflater;

    public HomeAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    public void setList(List<HomeNewsKejiBean.ResultBean.DataBean> list) {
        mData.clear();
        mData.addAll(list);
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder viewHolder = new ItemViewHolder(mInflater.inflate(R.layout.home_item_view_two, parent, false));
            return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       /* if (holder instanceof HeadViewHolder) {

        } else *//*if (holder instanceof ItemViewHolder) {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            HomeBean homeBean = mData.get(position);
            viewHolder.setData(homeBean.getData(), position);
        }*/if(holder instanceof ItemViewHolder){
            ItemViewHolder itemTwoViewHolder = (ItemViewHolder) holder;
            HomeNewsKejiBean.ResultBean.DataBean dataBean = mData.get(position);
            itemTwoViewHolder.setData(dataBean, position);
        } /*else {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.pull_to_refresh_loadmore_text.setText("上拉加载...");
                    break;
                case LOADING_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.pull_to_refresh_loadmore_text.setText("正在加载...");
                    break;
                case LOADING_NO:
                    footViewHolder.itemView.setVisibility(View.INVISIBLE);
                    break;
            }
        }*/
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    /**
     * 进行判断是普通Item视图还是FootView视图
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        /*if (position == 0) {
            return TYPE_HEAD;
        } else *//*if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {*/
            return TYPE_ITEM;
        //}
    }

    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView recy_title;
        TextView recy_time;
        TextView recy_yuenum;
        ImageView recy_two_img1;
        ImageView recy_two_img2;
        ImageView recy_two_img3;
        private final LinearLayout imgLayout;

        public ItemViewHolder(View itemView) {
            super(itemView);
            recy_title = itemView.findViewById(R.id.recy_title);
            recy_time = itemView.findViewById(R.id.recy_time);
            recy_yuenum = itemView.findViewById(R.id.recy_yuenum);
            recy_two_img1 = itemView.findViewById(R.id.recy_two_img1);
            recy_two_img2 = itemView.findViewById(R.id.recy_two_img2);
            recy_two_img3 = itemView.findViewById(R.id.recy_two_img3);
            imgLayout = itemView.findViewById(R.id.imgLayout);
        }

        public void setData(HomeNewsKejiBean.ResultBean.DataBean data, int position) {
            recy_title.setText(data.getTitle());
            recy_time.setText(data.getDate());
            recy_yuenum.setText("25");
            if (TextUtils.isEmpty(data.getThumbnail_pic_s()) && TextUtils.isEmpty(data.getThumbnail_pic_s02()) && TextUtils.isEmpty(data.getThumbnail_pic_s03())) {
                imgLayout.setVisibility(View.GONE);
            } else {
                imgLayout.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(data.getThumbnail_pic_s())) {
                    recy_two_img1.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(data.getThumbnail_pic_s()).into(recy_two_img1);
                } else {
                    recy_two_img1.setVisibility(View.INVISIBLE);
                    if (!TextUtils.isEmpty(data.getThumbnail_pic_s02())) {
                        Glide.with(mContext).load(data.getThumbnail_pic_s02()).into(recy_two_img1);
                    } else {
                        recy_two_img2.setVisibility(View.INVISIBLE);
                        if (!TextUtils.isEmpty(data.getThumbnail_pic_s03())) {
                            recy_two_img3.setVisibility(View.INVISIBLE);
                            Glide.with(mContext).load(data.getThumbnail_pic_s03()).into(recy_two_img1);
                        } else {
                            recy_two_img3.setVisibility(View.INVISIBLE);
                        }
                    }
                }

                if (!TextUtils.isEmpty(data.getThumbnail_pic_s02())) {
                    recy_two_img2.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(data.getThumbnail_pic_s02()).into(recy_two_img2);
                } else {
                    if (!TextUtils.isEmpty(data.getThumbnail_pic_s03())) {
                        recy_two_img3.setVisibility(View.INVISIBLE);
                        Glide.with(mContext).load(data.getThumbnail_pic_s03()).into(recy_two_img2);
                    } else {
                        recy_two_img2.setVisibility(View.INVISIBLE);
                        recy_two_img3.setVisibility(View.INVISIBLE);
                    }
                }
                if (!TextUtils.isEmpty(data.getThumbnail_pic_s03())) {
                    recy_two_img3.setVisibility(View.VISIBLE);
                    Glide.with(mContext).load(data.getThumbnail_pic_s03()).into(recy_two_img3);
                } else {
                    recy_two_img3.setVisibility(View.INVISIBLE);
                }

            }

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, NewsDetailActivity.class);
                    intent.putExtra("url",data.getUrl());
                    mContext.startActivity(intent);
                }
            });
        }
    }

}
